<?php

/**
 * Classe : UFR
 * @author Jeremy
 * @version 1.0
 */
class UFR
{

    /**
     * @var int
     */
    protected $Id; // INT(11)
    /**
     * @var string
     */
    protected $Nom;

    /**
     *
     * @param int $id
     */
    function __construct($id = 0)
    {
        if (isset($id) && !empty($id))
        {
            $sql = SQL::getInstance()->getConnection();

            $query = "	SELECT UFR.* 
						FROM UFR
						WHERE UFR.IdUFR = :id
					";
            $c = $sql->prepare($query);
            $c->bindParam(":id", $id, PDO::PARAM_INT);

            if ($c->execute())
            {
                $result = $c->fetch(PDO::FETCH_OBJ);

                $this->Id = $result->IdUFR;
                $this->Nom = $result->NomUFR;

                $this->errormsg = "UFR recuperee";
                $this->requeteOk = true;
            }
            else
            {
                $this->Id = 0;
                $this->Nom = "";

                $this->errormsg = "SQL Failed";
                $this->requeteOk = false;
            }
            $c->closeCursor();
        }
        else
        {
            $this->Id = 0;
            $this->Nom = "";

            $this->errormsg = "UFR vierge";
        }
    }

    /**
     *
     * @return bool
     */
    function delete()
    {
        if (!empty($this->IdTechnologie))
        {
            $sql = SQL::getInstance()->getConnection();

            $query = "	DELETE FROM UFR 
						WHERE IdUFR = :id
					";
            $c = $sql->prepare($query);
            $c->bindParam(":id", $this->Id, PDO::PARAM_INT);

            if ($c->execute())
            {
                return true;
            }
            else
            {
                $this->errormsg = $c->errorInfo();
                $this->requeteOk = false;
            }

            $c->closeCursor();
        }

        return false;
    }

    /**
     * Cree un UFR dans la base
     */
    public function insert()
    {
        $sql = SQL::getInstance()->getConnection();
        $query =
                'INSERT INTO UFR
		(
			NomUFR
		)
		VALUES
		(
			:nom
		)';
        $sql->beginTransaction();
        try
        {
            $c = $sql->prepare($query);

            $c->bindParam(':nom', $this->Nom, PDO::PARAM_STR);

            if ($c->execute())
            {
                $this->Id = $sql->lastInsertId();
                $sql->commit();
                $c->closeCursor();
                $this->requeteOk = true;
                $this->errormsg = "";
            }
            else
            {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
                $sql->rollback();
                $c->closeCursor();
                $this->Id = 0;
            }
        }
        catch (PDOException $Exception)
        {
            $this->requeteOk = false;
            $this->errormsg = $c->errorInfo();
            $sql->rollback();
            $c->closeCursor();
            $this->Id = 0;
        }
    }

    /**
     * Met a jour un UFR dans la base
     */
    public function update()
    {
       $sql = SQL::getInstance()->getConnection();
        $query =
                'UPDATE UFR
		SET 
			NomUFR		= :nom
		WHERE 
			IdUFR		= :id';
        $sql->beginTransaction();
        try
        {
            $c = $sql->prepare($query);

            $c->bindParam(':id', $this->Id, PDO::PARAM_INT);

            $c->bindParam(':nom', $this->Nom, PDO::PARAM_STR);

            if ($c->execute())
            {
                $this->requeteOk = true;
                $this->errormsg = "";
                $sql->commit();
                $c->closeCursor();
            }
            else
            {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
                $sql->rollback();
                $c->closeCursor();
            }
        }
        catch (PDOException $Exception)
        {
            $this->requeteOk = false;
            $this->errormsg = $c->errorInfo();
            $sql->rollback();
            $c->closeCursor();
        }
    }

    /**
     *
     * @return array Array d'UFR
     */
    static public function getList()
    {
        $sql = SQL::getInstance()->getConnection();

        $query = "	SELECT *
					FROM UFR
				";
        $c = $sql->query($query);
        $result = $c->fetchAll(PDO::FETCH_OBJ);
        if (!empty($result))
        {
            $ufr = array();
            foreach ($result as $k => $v)
            {
                $ufr[$v->IdUFR] = $v->NomUFR;
            }
            return $ufr;
        }
        return array();
    }

    /**
     * @static
     * @return int Nombre d'ufr differentes
     */
    static public function globalCount()
    {
        $sql = SQL::getInstance()->getConnection();

        $query = "SELECT COUNT(IdUFR) as count FROM UFR";
        $c = $sql->query($query);
        $result = $c->fetch(PDO::FETCH_OBJ);
        return $result->count;
    }

    /**
     *
     * @return int
     */
    public function getId()
    {
        return $this->Id;
    }

    /**
     *
     * @return string
     */
    public function getNom()
    {
        return $this->Nom;
    }

    /**
     *
     * @param string $value
     * @return array
     */
    public function setNom($value)
    {
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        $errTab = array("boolOk" => true, "errmsg" => "");

        if (isset($value) && $value != "" && strlen($value) <= 255)
        {
            $this->Nom = htmlentities($value);
        }
        else
        {
            if (!isset($value))
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Le nom ne peut etre null !";
            }
            if (isset($value) && $value == "")
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Le nom ne peut etre vide !";
            }
            else
            {
                if (isset($value) && strlen($value) > 255)
                {
                    $errTab["boolOk"] = false;
                    $errTab["errmsg"] = "Le nom doit etre compris entre 1 et 255 caracteres !";
                }
                else
                {
                    $errTab["boolOk"] = false;
                    $errTab["errmsg"] = "Ce nom n'est pas valide !";
                }
            }

            $this->Nom = "";
        }
        return $errTab;
    }

}

?>